//
//  HeaderView.m
//  SUZHOUBANK
//
//  Created by saberLily on 16/4/19.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "HeaderView.h"
#define LabelHeight 40
static NSString *const kUITableViewCellIndentifier = @"cellIdentifier";
@interface HeaderView()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isSelecedHeader;
}
@property (nonatomic,strong) UITableView *tableView; /**< 表格 */
@property (nonatomic,assign) NSInteger selectedRow; /**< 选好的Row */
@end
@implementation HeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDataSource];
        [self initializeUserInterface];

    }
    return self;
}
#pragma mark - initializeDataSource 初始化数据源
-(void)initializeDataSource{
    
    _isSelecedHeader = false;
    
}
-(void)setHeaderTitle{
    
    if ([self.headerBtn titleForState:UIControlStateNormal].length == 0) {
        self.selectedRow = 0;
    }else{
        
    }
    
}
-(void)reloadTableViewData{
    [self.tableView reloadData];
}
#pragma mark - initializeUserInterface 初始化用户界面
-(void)initializeUserInterface{
    [self addSubview:self.headerBtn];
    [self addSubview:self.tableView];
}
#pragma mark *** Delegate ***
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.tableView.backgroundColor = _isSelecedHeader?[UIColor whiteColor]:[UIColor clearColor];
    
    [UIView animateWithDuration:0.35f animations:^{
        self.tableView.frame = _isSelecedHeader?CGRectMake(0,LabelHeight, self.bounds.size.width, _dataSource.count>6?6*LabelHeight:LabelHeight*_dataSource.count):CGRectMake(0, LabelHeight, self.bounds.size.width, LabelHeight);
    }];
    
    
    return _isSelecedHeader?_dataSource.count:0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIndentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIndentifier];
    }
    
    cell.textLabel.text = _dataSource[indexPath.row];
    
   
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedRow = indexPath.row;
    
    _isSelecedHeader = !_isSelecedHeader;
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
    if (_delegate && [_delegate respondsToSelector:@selector(HeadView:didSelectedRow:didSelectedHeader:dataCount:)]) {
        [_delegate HeadView:self didSelectedRow:indexPath.row didSelectedHeader:_isSelecedHeader dataCount:_dataSource.count];
    }
    
}


#pragma mark *** methods ***
-(void)didSelectedHeader:(UIButton *)sender{
    _isSelecedHeader = !_isSelecedHeader;
  
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    
    if (_delegate && [_delegate respondsToSelector:@selector(HeadView:didSelectedHeader:)]) {
        [_delegate HeadView:self didSelectedHeader:_isSelecedHeader];
    }
    
    
}
#pragma mark *** setters ***
-(void)setSelectedRow:(NSInteger )selectedRow{
    _selectedRow = selectedRow;
    
    [self.headerBtn setTitle:_dataSource[_selectedRow] forState:UIControlStateNormal];
}
#pragma mark *** getters ***
-(UIButton *)headerBtn{
    if (!_headerBtn) {
        _headerBtn =[UIButton buttonWithType:UIButtonTypeCustom];
         _headerBtn.frame = CGRectMake(0, 0, self.bounds.size.width, LabelHeight);
        
//        [_headerBtn setBackgroundImage:[UIImage imageNamed:@"对公产品_09"] forState:UIControlStateNormal];
        NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"对公产品_09" ofType:@"png"];
        UIImage *im = [[UIImage alloc]initWithContentsOfFile:imagePath];
        [_headerBtn setBackgroundImage:im forState:UIControlStateNormal];
        
        [_headerBtn setTitle:_dataSource[0] forState:UIControlStateNormal];
        
        [_headerBtn addTarget:self action:@selector(didSelectedHeader:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerBtn;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,LabelHeight, self.bounds.size.width, LabelHeight*5)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kUITableViewCellIndentifier];
    }
    return _tableView;
}
@end
